import { h } from 'vue'
import plantImg from '@/assets/img/plant.png'
import examImg from '@/assets/img/exam.png'
import statisticsImg from '@/assets/img/statistics.png'
import studentManageImg from '@/assets/img/studentManage.png'
import messageImg from '@/assets/img/message.png'
import examPaperImg from '@/assets/img/examPaper.png'
import systemImg from '@/assets/img/system.png'

/**
 * 菜单路由
 */
/* todo 【请务必确保路由的name唯一,且访问路径采用小驼峰命名方式】  */
const menuRoutes = [
  {
    path: '/plant',
    name: 'plant',
    redirect: '/plant/jinfo',
    meta: {
      title: '植物列表',
      layout: 'basic',
      icon: h('img', { src: plantImg, style: 'width: 14px' }),
      keepAlive: false
    },
    children: [
      {
        path: 'learn',
        name: 'plantLearn',
        component: () => import('@/views/plant/learn/index.vue'),
        meta: {
          title: '理论教材学习植物',
          layout: 'basic',
          keepAlive: false,
        }
      },
      {
        path: 'school',
        name: 'plantSchool',
        component: () => import('@/views/plant/school/index.vue'),
        meta: {
          title: '校园救生植物',
          layout: 'basic',
          keepAlive: false,
        }
      },
      {
        path: 'jinfo',
        name: 'plantJinfo',
        component: () => import('@/views/plant/jinfo/index.vue'),
        meta: {
          title: '金佛山野外救生植物',
          layout: 'basic',
          keepAlive: false,
        }
      },
      {
        path: 'tibetan',
        name: 'plantTibetan',
        component: () => import('@/views/plant/tibetan/index.vue'),
        meta: {
          title: '西南地区救生植物',
          layout: 'basic',
          keepAlive: false,
        }
      },
      {
        path: 'add',
        name: '',
        component: () => import('@/views/plant/add.vue'),
        meta: {
          title: '编辑植物',
          layout: 'basic',
          keepAlive: false,
          isHide: true
        }
      },
      {
        path: 'detail',
        name: '',
        component: () => import('@/views/plant/detail.vue'),
        meta: {
          title: '植物详情',
          layout: 'basic',
          keepAlive: false,
          isHide: true
        }
      }
    ]
  },
  {
    path: '/message',
    name: 'message',
    meta: {
      title: '留言板',
      layout: 'basic',
      icon: h('img', { src: messageImg, style: 'width: 14px' }),
      keepAlive: false
    },
    component: () => import('@/views/message/index.vue'),
  },
  {
    path: '/exam',
    name: 'exam',
    redirect: '/exam/list',
    meta: {
      title: '考试',
      layout: 'basic',
      icon: h('img', { src: examImg, style: 'width: 14px' }),
      keepAlive: false
    },
    children: [
      {
        path: 'list',
        name: 'examList',
        component: () => import('@/views/exam/index.vue'),
        meta: {
          title: '考试列表',
          layout: 'basic',
          keepAlive: false,
        }
      },
      {
        path: 'examPaper',
        name: 'examPaper',
        meta: {
          title: '试卷管理',
          layout: 'basic',
          // icon: h('img', { src: examPaperImg, style: 'width: 14px' }),
          keepAlive: false
        },
        component: () => import('@/views/examPaper/index.vue'),
      },
      {
        path: 'select',
        name: '',
        component: () => import('@/views/exam/select.vue'),
        meta: {
          title: '选择题',
          layout: 'basic',
          keepAlive: false,
          isHide: true,
        }
      },
    ]
  },
  {
    path: '/statistics',
    name: 'statistics',
    meta: {
      title: '统计',
      layout: 'basic',
      icon: h('img', { src: statisticsImg, style: 'width: 14px' }),
      keepAlive: false
    },
    component: () => import('@/views/statistics/index.vue'),
  },
  {
    path: '/student',
    name: 'student',
    meta: {
      title: '学生管理',
      layout: 'basic',
      icon: h('img', { src: studentManageImg, style: 'width: 14px' }),
      keepAlive: false
    },
    component: () => import('@/views/student/index.vue'),
  },
  // {
  //   path: '/examPaper',
  //   name: 'examPaper',
  //   meta: {
  //     title: '试卷管理',
  //     layout: 'basic',
  //     icon: h('img', { src: examPaperImg, style: 'width: 14px' }),
  //     keepAlive: false
  //   },
  //   component: () => import('@/views/examPaper/index.vue'),
  // },
  {
    path: '/system',
    name: 'system',
    meta: {
      title: '系统管理',
      layout: 'basic',
      icon: h('img', { src: systemImg, style: 'width: 14px' }),
      keepAlive: false
    },
    children: [
      {
        path: 'account',
        name: 'systemAccount',
        component: () => import('@/views/system/account/index.vue'),
        meta: {
          title: '账号管理',
          layout: 'basic',
          keepAlive: false,
        }
      },
      {
        path: 'account',
        name: 'systemRole',
        component: () => import('@/views/system/role/index.vue'),
        meta: {
          title: '角色管理',
          layout: 'basic',
          keepAlive: false,
        }
      }
    ]
  }
]


/**
 * 给路由添加super
 */
function routesAddSuper(routes: any[], superName?: string[] | string) {
  routes.forEach((route) => {
    if (!route.meta) {
      route.meta = {}
    }
    if (superName) {
      if (Array.isArray(superName)) {
        if (Array.isArray(route.meta.super)) {
          route.meta.super.push(...superName)
        } else {
          route.meta.super = [...superName]
        }
      } else {
        if (Array.isArray(route.meta.super)) {
          route.meta.super.push(superName)
        } else {
          route.meta.super = [superName]
        }
      }
    }
    if (route?.children?.length) {
      let _superNames = []
      if (route?.meta?.super) {
        _superNames = [...route.meta.super, route.name]
      } else {
        _superNames.push(route.name)
      }
      routesAddSuper(route.children, _superNames)
    }
  })
}

routesAddSuper(menuRoutes)

export { menuRoutes }
